import { _get, _post, _post1 } from "../../utils/request";
import util from "../../utils/util";
const app = getApp();
Page({
    data: {
        active_add: false,
        items: [
            // { value: "1", text: "AngularJS" },
            // { value: "2", text: "React" },
            // { value: "3", text: "Polymer" },
            // { value: "4", text: "Vue.js" },
            // { value: "5", text: "Ember.js" },
            // { value: "6", text: "Backbone.js" },
            // { value: 'all', text: '全选' }
        ],
        items1: [],
    },
    getValue(obj) {
        let arr = [];
        for (let k in obj) {
            arr.push(obj[k]);
        }
        return arr;
    },
    addShow() {
        this.setData({
            active_add: true,
        });
        this.getMbxq1();
    },
    close_add() {
        this.setData({
            active_add: false,
        });
    },
    search1(e) {
        this.getMbxq1(e);
    },
    getMbxq() {
        let params = { tempId: app.temp_id };
        _get(`/item/selectByTempId`, params).then((res) => {

            let list = res.data.map((v) => {
                v.templateItemSkillAskVOList.forEach((item) => {
                    item.checked = true;
                });
                return v;
            });
            console.log("mbxq", list);
            this.setData({
                items: list,
                title: res.title,
            });
        });
    },
    getMbxq1(keywords = "") {
        let params = { name: keywords, templateId: 1 };
        _get(`/item/selectBesidesTempId`, params).then((res) => {
            console.log("mbxq1", res);
            this.setData({
                items1: res.data,
            });
        });
    },
    onSubmit(e) {
        let params = {
            itemId: app.jcx_id,
            tempItemIdList: this.getValue(e.detail.value).flat(Infinity),
        };
        console.log("提交技术要求", e, params);
        _post1("/item/insertItem", JSON.stringify(params)).then((res) => {
            console.log("提交成功", res);
            dd.reLaunch({
                url: "/pages/mytest/test_list/test_list",
            });
        });
        //一个表单不同按钮触发不同事件的方法
        // if (e.buttonTarget.dataset.type == 1) {
        //     console.log(1);
        // } else if (e.buttonTarget.dataset.type == 2) {
        //     console.log(2);
        // }
    },
    onSubmit_add(e) {
        let addArr = e.detail.value.jsyq;

        addArr.map((item) => {
            item.checked = true;
            return item;
        });
        console.log(addArr);
        let arr = addArr.concat(this.data.items);
        // console.log('增加技术要求', e.detail.value, arr);
        this.setData({
            items: arr,
            active_add: false,
        });

        // _get(`/confirmWorker/affirmWorker`, params).then(res => {
        //     console.log(res);
        //     dd.navigateTo({
        //         url: '/pages/choose_mb/choose_mb'
        //     })
        // })
    },
    check(e) {
        // console.log('checked', e);
        this.data.items[e.currentTarget.dataset.index1].templateItemSkillAskVOList[e.currentTarget.dataset.index2].checked = e.detail.value;
        this.setData({
            items: this.data.items,
        });
        console.log('多选框', this.data.items);
    },
    onLoad() {
        dd.setNavigationBar({
            title: "选择技术要求",
            backgroundColor: "#142e4f",
            success() {},
            fail() {},
        });
        this.getMbxq();
    },
});